package com.imay.live.view.custom;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.base.util.ViewUtil;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jeaman on 2017/3/16.
 * 首页底部action按钮
 */

public class MainActionView extends RelativeLayout {
    private final static int[] BUTTON_TEXT_RES = new int[]{R.string.main_action_short_live,R.string.main_action_live, R.string.main_action_photo, R.string.main_action_video};
    private final static int[] BUTTON_ICON_RES = new int[]{R.drawable.icon_main_action_short_live,R.drawable.icon_main_action_live, R.drawable.icon_main_action_photo, R.drawable.icon_main_action_video};

    private final static int BUTTON_COUNT = 4;
    private final static int TEXT_SIZE = 15;
    private final static int DURATION_MILLIS = 150;

    private final static int STATE_HIDED = 0;
    private final static int STATE_HIDING = 1;
    private final static int STATE_SHOWING = 2;
    private final static int STATE_SHOWED = 3;

    private int mState = STATE_HIDED;

    private int mBottomMargin;
    private OnActionClickListener mActionClickListener;
    private List<View> mButtonViewList = new ArrayList<>(BUTTON_COUNT);

    public MainActionView(Context context) {
        super(context);
        init(context);
    }

    public MainActionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MainActionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MainActionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);

        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.BOTTOM);

        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.bf_03kbxz_hu_pnl);
        layout.addView(imageView,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout mL = new LinearLayout(context);
        mL.setBackgroundColor(Color.WHITE);
        mL.setGravity(Gravity.BOTTOM);
        mL.setOrientation(LinearLayout.HORIZONTAL);
        mL.setWeightSum(BUTTON_COUNT);

        mBottomMargin = getResources().getDimensionPixelOffset(R.dimen.action_button_bottom);
        int drawablePadding = getResources().getDimensionPixelOffset(R.dimen.action_button_drawable_padding);

        for (int i = 0; i < BUTTON_TEXT_RES.length; i++) {
            final int position = i;
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setVisibility(GONE);
            linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
            layoutParams.setMargins(0, 0, 0, mBottomMargin);
            linearLayout.setLayoutParams(layoutParams);

            Drawable drawable = getResources().getDrawable(BUTTON_ICON_RES[i]);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

            TextView textView = new TextView(context);
            textView.setText(BUTTON_TEXT_RES[i]);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(getResources().getColor(R.color.text_ff50));
            textView.setCompoundDrawables(null, drawable, null, null);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TEXT_SIZE);
            textView.setCompoundDrawablePadding(drawablePadding);
            textView.setTag(i);
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mActionClickListener != null) {
                        mActionClickListener.onActionClick(position);
                    }
                }
            });

            linearLayout.addView(textView);
            mL.addView(linearLayout);
            mButtonViewList.add(linearLayout);
            ViewUtil.measureView(linearLayout);
        }
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM);

        layout.addView(mL);
        addView(layout, layoutParams);

        int mCloseBottomMargin = getResources().getDimensionPixelOffset(R.dimen.action_button_close_bottom);
        ImageView imageView1 = new ImageView(context);
        imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView1.setImageResource(R.mipmap.btn_03kbxz_close);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams1.addRule(ALIGN_PARENT_BOTTOM);
        layoutParams1.addRule(CENTER_HORIZONTAL);
        layoutParams1.setMargins(0, 0, 0, mCloseBottomMargin);
        addView(imageView1, layoutParams1);
    }

    public void showActionButton() {
        mState = STATE_SHOWING;
        for (int i = 0; i < mButtonViewList.size(); i++) {
            final View view = mButtonViewList.get(i);
            final Animation animation = new TranslateAnimation(0, 0, mBottomMargin + view.getMeasuredHeight(), 0);
            view.setVisibility(View.GONE);
            animation.setDuration(DURATION_MILLIS);
            animation.setInterpolator(new DecelerateInterpolator());

            animation.setFillEnabled(true);
            animation.setFillAfter(true);
            if (i == mButtonViewList.size() - 1) {
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mState = STATE_SHOWED;
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.startAnimation(animation);
                    view.setVisibility(View.VISIBLE);
                }
            }, i * (DURATION_MILLIS - 50));
        }
        setVisibility(VISIBLE);
    }

    public void hideActionButton(final Runnable runnable) {
        //确保显示动画完全显示才执行
        if (mState != STATE_SHOWED) {
            return;
        }
        mState = STATE_HIDING;
        for (int i = 0; i < mButtonViewList.size(); i++) {
            final View view = mButtonViewList.get(i);
            final Animation animation = new TranslateAnimation(0, 0, 0, getBottom() - view.getTop());
            animation.setDuration(DURATION_MILLIS);
            animation.setInterpolator(new AccelerateInterpolator());
            animation.setFillEnabled(true);
            animation.setFillAfter(true);
            if (i == mButtonViewList.size() - 1) {
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        setGone();
                        if (runnable != null) {
                            runnable.run();
                        }
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.startAnimation(animation);
                }
            }, i * (DURATION_MILLIS - 50));
        }
    }

    private void setGone() {
        setVisibility(GONE);
        mState = STATE_HIDED;
    }

    public void setOnActionClickListener(OnActionClickListener listener) {
        mActionClickListener = listener;
    }

    public interface OnActionClickListener {
        void onActionClick(int position);
    }
}
